package com.bjsxt.tank;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class TankClient extends Frame {
	public static final int GAME_WIDTH = 3840;
	public static final int GAME_HEIGHT = 2160;
	public static final int Button_x = 200;
	public static final int Button_y = 250;
	public static final int Button_W = 100;
	public static final int Button_H = 30;

	Tank myTank = new Tank(50, 50, true, Direction.STOP, this);
	Wall w1 = new Wall(100, 200, 20, 150, this),
			w2 = new Wall(300, 100, 300, 20, this),
			w3 = new Wall(1000, 200, 20, 700, this);

	List<Explode> explodes = new ArrayList<>();
	List<Missile> missiles = new ArrayList<>();
	List<Tank> tanks = new ArrayList<>();
	Image offScreenImage = null;
	private static Toolkit tk = Toolkit.getDefaultToolkit();
	static Image backGround = null;
	Blood b = new Blood();
	private int round;
	private boolean isGameOver = false;

	// 修复1：将resetGame提升为成员方法
	private void resetGame() {
		tanks.clear();
		int initTankCount = Integer.parseInt(PropertyMgr.getProperty("reProduceTankCount"));
		for (int i = 0; i < initTankCount; i++) {
			tanks.add(new Tank(50 + 40 * (i + 1), 50, false, Direction.D, this));
		}
		myTank.setX(50);
		myTank.setY(50);
		myTank.setLife(100);
		missiles.clear();
		explodes.clear();
		round = 0;
		isGameOver = false;
		repaint();
	}

	public void paint(Graphics g) {
		g.drawString("missiles count:" + missiles.size(), 10, 50);
		g.drawString("explodes count:" + explodes.size(), 10, 70);
		g.drawString("tanks    count:" + tanks.size(), 10, 90);
		g.drawString("tanks     life:" + myTank.getLife(), 10, 110);

		if (!isGameOver && tanks.isEmpty()) {
			round++;
			if (round < 3) {
				for (int i = 0; i < Integer.parseInt(PropertyMgr.getProperty("reProduceTankCount")); i++) {
					tanks.add(new Tank(50, 50, false, Direction.D, this)); // 修复2：统一使用Direction.D
				}
			} else {
				isGameOver = true;
			}
		}

		// 修复3：移除此处的事件监听器注册
		for (int i = 0; i < missiles.size(); i++) {
			Missile m = missiles.get(i);
			m.hitTanks(tanks);
			m.hitTank(myTank);
			m.hitWall(w1);
			m.hitWall(w2);
			m.hitWall(w3);
			m.draw(g);
		}

		for (int i = 0; i < explodes.size(); i++) {
			Explode e = explodes.get(i);
			e.draw(g);
		}

		for (int i = 0; i < tanks.size(); i++) {
			Tank t = tanks.get(i);
			t.draw(g);
		}

		myTank.draw(g);
		w1.draw(g);
		w2.draw(g);
		w3.draw(g);
	}

	public void update(Graphics g) {
		if (offScreenImage == null) {
			offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
		}
		Graphics gOffScreen = offScreenImage.getGraphics();
		Color c = gOffScreen.getColor();
		gOffScreen.setColor(Color.BLACK);

		if (!isGameOver) {
			gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		}
		gOffScreen.drawImage(backGround, 0, 0, null);

		if (!isGameOver) {
			for (Tank t : tanks) t.move();
			for (Missile m : missiles) m.move();
		} else {
			gOffScreen.setColor(Color.GREEN);
			gOffScreen.setFont(new Font("宋体", Font.BOLD, 30));
			gOffScreen.drawString("胜利！", 200, 200);
			gOffScreen.setColor(Color.BLUE);
			gOffScreen.drawRect(Button_x, Button_y, Button_W, Button_H);
			gOffScreen.drawString("重新开始", Button_x + 10, Button_y + 20);
		}

		gOffScreen.setColor(c);
		paint(gOffScreen);
		g.drawImage(offScreenImage, 0, 0, null);
	}

	static {
		backGround = tk.getImage(TankClient.class.getClassLoader().getResource("images/background.jpg"));
	}

	public void lauchFrame() {
		// 修复4：事件监听器统一在此注册
		this.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (isGameOver) {
					int x = e.getX();
					int y = e.getY();
					if (x >= Button_x && x <= Button_x + Button_W &&
							y >= Button_y && y <= Button_y + Button_H) {
						resetGame();
					}
				}
			}
		});

		int initTankCount = Integer.parseInt(PropertyMgr.getProperty("initTankCount"));
		for (int i = 0; i < initTankCount; i++) {
			tanks.add(new Tank(50 + 40 * (i + 1), 50, false, Direction.D, this));
		}

		this.setSize(GAME_WIDTH, GAME_HEIGHT);
		this.setTitle("TankWar");
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		this.setUndecorated(true);
		this.setBackground(Color.GREEN);
		this.addKeyListener(new KeyMonitor());
		setVisible(true);
		new Thread(new PaintThread()).start();
	}

	public static void main(String[] args) {
		TankClient tc = new TankClient();
		tc.lauchFrame();
	}

	private class PaintThread implements Runnable {
		public void run() {
			while (true) {
				repaint();
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private class KeyMonitor extends KeyAdapter {
		public void keyReleased(KeyEvent e) {
			myTank.keyReleased(e);
		}
		public void keyPressed(KeyEvent e) {
			myTank.keyPressed(e);
		}
	}}
